package com.xiaoniu.forum.service.impl;

import com.xiaoniu.forum.common.constant.ForumConstant;
import com.xiaoniu.forum.common.event.ForumEvent;
import com.xiaoniu.forum.common.utils.CommonQueryVo;
import com.xiaoniu.forum.common.utils.QueryWrapperUtils;
import com.xiaoniu.forum.dao.CommunityTopicDao;
import com.xiaoniu.forum.dao.MemberDao;
import com.xiaoniu.forum.dao.MemberNospeakDao;
import com.xiaoniu.forum.entity.CommunityTopicEntity;
import com.xiaoniu.forum.entity.MemberEntity;
import com.xiaoniu.forum.entity.MemberNospeakEntity;
import com.xiaoniu.forum.entity.vo.CommunityTopicCommentPassVo;
import com.xiaoniu.forum.entity.vo.PassListVo;
import com.xiaoniu.forum.service.MemberMessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoniu.forum.common.utils.PageUtils;
import com.xiaoniu.forum.common.utils.Query;

import com.xiaoniu.forum.dao.CommunityTopicCommentDao;
import com.xiaoniu.forum.entity.CommunityTopicCommentEntity;
import com.xiaoniu.forum.service.CommunityTopicCommentService;
import org.springframework.util.StringUtils;


@Service("communityTopicCommentService")
public class CommunityTopicCommentServiceImpl extends ServiceImpl<CommunityTopicCommentDao, CommunityTopicCommentEntity> implements CommunityTopicCommentService {

    @Autowired
    private CommunityTopicDao communityTopicDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private MemberNospeakDao memberNospeakDao;

    @Autowired
    private MemberMessageService messageService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CommunityTopicCommentEntity> page = this.page(
                new Query<CommunityTopicCommentEntity>().getPage(params),
                new QueryWrapper<CommunityTopicCommentEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, QueryWrapper<CommunityTopicCommentEntity> wrapper) {
        IPage<CommunityTopicCommentEntity> page = this.page(
                new Query<CommunityTopicCommentEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils getListForPass(Map<String, Object> params) {
        QueryWrapper<CommunityTopicCommentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("pass","2");
        wrapper.eq("status","1");

        PageUtils page = this.queryPage(params,wrapper);

        List communityTopicCommentList = page.getList();

        List<PassListVo> list = new ArrayList<>();

        //将数据列表封装为passListVo
        for (Object obj : communityTopicCommentList) {
            CommunityTopicCommentEntity communityTopicCommentEntity = (CommunityTopicCommentEntity) obj;

            PassListVo passListVo = new PassListVo();

            //封装用户信息
            MemberEntity memberEntity = memberDao.selectById(communityTopicCommentEntity.getUserId());
            if (memberEntity != null) {
                passListVo.setUserName(memberEntity.getUsername() + "::" + memberEntity.getNickname());
                passListVo.setMemberStatus(memberEntity.getStatus());
                QueryWrapper<MemberNospeakEntity> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("member_id",memberEntity.getId());
                MemberNospeakEntity memberNospeakEntity = memberNospeakDao.selectOne(queryWrapper);
                if (memberNospeakEntity != null){
                    passListVo.setNospeak("1");
                }
            }

            //封装评论相关信息
            passListVo.setCreateTime(communityTopicCommentEntity.getCreateTime());
            passListVo.setContent(communityTopicCommentEntity.getReplyText());
            passListVo.setMemberId(communityTopicCommentEntity.getUserId());
            passListVo.setId(communityTopicCommentEntity.getId());
            list.add(passListVo);
        }

        page.setList(list);

        return page;
    }

    @Override
    public void passed(String topicCommentId) {
        CommunityTopicCommentEntity communityTopicCommentEntity = new CommunityTopicCommentEntity();
        communityTopicCommentEntity.setId(topicCommentId);
        communityTopicCommentEntity.setPass("1");
        baseMapper.updateById(communityTopicCommentEntity);
        CommunityTopicCommentEntity topicCommentEntity = baseMapper.selectById(topicCommentId);
        ForumEvent forumEvent = new ForumEvent();
        forumEvent
                .setTriggerId(ForumConstant.SYSTEM_ID+"")  //事件触发者（发送人）
                .setTargetId(topicCommentEntity.getUserId())  //事件目标者（接受者）
                .setAction(ForumConstant.APPROVED_ACTION)  //审核通过
                .setData(ForumConstant.KEYNAME_ENTITY_TYPE,ForumConstant.COMMENT_ENTITY_TYPE) //其他数据
                .setData(ForumConstant.KEYNAME_ENTITY_ID, topicCommentId); //其他数据
        messageService.sendEvent(forumEvent);

        //话题评论数加1
        CommunityTopicEntity communityTopicEntity = communityTopicDao.selectById(topicCommentEntity.getThreadsId());
        communityTopicEntity.setReplyCount(communityTopicEntity.getReplyCount() + 1);
        communityTopicDao.updateById(communityTopicEntity);
    }

    @Override
    public void passedFailed(String topicCommentId, String message) {
        CommunityTopicCommentEntity communityTopicCommentEntity = new CommunityTopicCommentEntity();
        communityTopicCommentEntity.setId(topicCommentId);
        communityTopicCommentEntity.setPass("1");
        baseMapper.updateById(communityTopicCommentEntity);
        CommunityTopicCommentEntity topicCommentEntity = baseMapper.selectById(topicCommentId);
        ForumEvent forumEvent = new ForumEvent();
        forumEvent
                .setTriggerId(ForumConstant.SYSTEM_ID+"")  //事件触发者（发送人）
                .setTargetId(topicCommentEntity.getUserId())  //事件目标者（接受者）
                .setAction(ForumConstant.NOT_APPROVED_ACTION)  //审核未通过
                .setData(ForumConstant.KEYNAME_ENTITY_TYPE,ForumConstant.COMMENT_ENTITY_TYPE) //其他数据
                .setData(ForumConstant.KEYNAME_ENTITY_ID, topicCommentId)
                .setData(ForumConstant.KEYNAME_RESON,message);//其他数据
        messageService.sendEvent(forumEvent);
    }

    @Override
    public PageUtils queryPage(CommonQueryVo queryVo) {

        //将多条件封装成queryWrapper
        QueryWrapper<CommunityTopicCommentEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(queryVo.getAnd()) || !StringUtils.isEmpty(queryVo.getOr())) {
            wrapper = QueryWrapperUtils.createQueryWrapper(queryVo, CommunityTopicCommentEntity.class);
        }

        IPage page = this.page(new Query<CommunityTopicCommentEntity>().getPage(queryVo.getPageParams()),wrapper);

        //封装成VO
        List<CommunityTopicCommentEntity> communityTopicCommentEntityList = page.getRecords();

        List<CommunityTopicCommentPassVo> list = new ArrayList<>();

        for (CommunityTopicCommentEntity communityTopicCommentEntity : communityTopicCommentEntityList) {
            CommunityTopicCommentPassVo communityTopicCommentPassVo = new CommunityTopicCommentPassVo();
            //拷贝相同属性名的字段
            BeanUtils.copyProperties(communityTopicCommentEntity, communityTopicCommentPassVo);
            //查询用户id
            MemberEntity memberEntity = memberDao.selectById(communityTopicCommentEntity.getUserId());
            //封装用户信息
            if (memberEntity != null){
                communityTopicCommentPassVo.setMemberName(memberEntity.getUsername()+ "::" + memberEntity.getNickname());
            }

            //封装其他信息
            communityTopicCommentPassVo.setContent(communityTopicCommentEntity.getReplyText());
            communityTopicCommentPassVo.setTopicId(communityTopicCommentEntity.getThreadsId());

            list.add(communityTopicCommentPassVo);
        }

        page.setRecords(list);

        return new PageUtils(page);
    }

    @Override
    public void disableById(String topicCommentId) {
        CommunityTopicCommentEntity topicCommentEntity = new CommunityTopicCommentEntity();
        topicCommentEntity.setId(topicCommentId);
        topicCommentEntity.setStatus("0");
        baseMapper.updateById(topicCommentEntity);
    }

    @Override
    public void enabledById(String topicCommentId) {
        CommunityTopicCommentEntity topicCommentEntity = new CommunityTopicCommentEntity();
        topicCommentEntity.setId(topicCommentId);
        topicCommentEntity.setStatus("1");
        baseMapper.updateById(topicCommentEntity);
    }

}
